package com.zznode.redsun.smartcrm.business.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.csp.domain.commons.query.JdbcFinderManager;
import com.zznode.csp.domain.commons.query.Page;
import com.zznode.csp.domain.commons.util.DomainBeanUtils;
import com.zznode.csp.domain.commons.util.MapCreaterBuilder;
import com.zznode.redsun.commons.dto.ConsultDto;
import com.zznode.redsun.commons.dto.RecordQuery;
import com.zznode.redsun.commons.sercurity.shiro.LoginUser;
import com.zznode.redsun.commons.utils.GlobalUtils;
import com.zznode.redsun.smartcrm.business.service.ConsultService;
import com.zznode.redsun.smartcrm.model.ConsultRecord;
import com.zznode.redsun.smartcrm.model.PersonalInfo;

@Service
public class ConsultServiceImpl implements ConsultService {
	@Autowired
	private JdbcFinderManager jdbcFinderManager;
	@Autowired
	private EntityManager entityManager;

	@Override
	public void save(ConsultDto dto, LoginUser<Long> loginUser) {
		Assert.notNull(dto, "Saved instance can't be null.");
		if (null == dto.getId()) {
			ConsultRecord entity = dto.convert();
			entity.setCreateUser(DomainBeanUtils.create(PersonalInfo.class, loginUser.getUserId()));
			this.entityManager.save(entity);
		} else {
			ConsultRecord entity = this.entityManager.get(ConsultRecord.class, dto.getId());
			dto.update(entity);
			this.entityManager.update(entity);
		}
	}

	@Override
	public ConsultDto get(Long id) {
		ConsultRecord entity = this.entityManager.get(ConsultRecord.class, id);
		if (null != entity) {
			return ConsultDto.create(entity);
		} else {
			return null;
		}
	}

	@Override
	public Page<ConsultDto> query(RecordQuery params, int pageNumber, int pageSize) {
		String querySql = this.jdbcFinderManager.getQueryStringByStatementKey("ConsultService_query");
		MapCreaterBuilder paramsBuilder = MapCreaterBuilder.create();
		if (null != params) {
			querySql = params.buildSql(querySql, paramsBuilder);
		}
		querySql += " order by t1.create_date desc";
		return jdbcFinderManager.queryForPage(querySql, paramsBuilder.map(), new RowMapper<ConsultDto>() {

			@Override
			public ConsultDto mapRow(ResultSet rs, int arg1) throws SQLException {
				ConsultDto dto = new ConsultDto();
				dto.setId(rs.getLong("id"));
				dto.setCustomerId(rs.getLong("customer_id"));
				dto.setCustomerCode(rs.getString("customer_code"));
				dto.setCustomerName(rs.getString("customer_name"));
				dto.setProductId(rs.getLong("product_id"));
				dto.setProductCode(rs.getString("product_code"));
				dto.setProductName(rs.getString("product_name"));
				dto.setTitle(rs.getString("title"));
				dto.setContent(rs.getString("content"));
				dto.setCreateTime(rs.getTimestamp("create_date"));
				dto.setCreateTimeString(GlobalUtils.formartDateTime(dto.getCreateTime()));
				dto.setCreateUserId(rs.getLong("create_user"));
				dto.setCreateUserName(entityManager.get(PersonalInfo.class, dto.getCreateUserId()).getName());

				return dto;
			}
		}, pageNumber, pageSize);
	}

}
